/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.webos.java.base.hsqldb.migration;

/**
 *
 * @author Kostya
 */
public class RowDefinition {
    private String name = "";
    private String type = "";
    private boolean primary = false;
    private boolean identity = false;
    private String defaultValue = null;
    private RowDefinition reference = null;
    private TableDefinition table = null;

    public RowDefinition(String aName, String aType) {
        setName(aName);
        setType(aType);
    }

    public RowDefinition(String aName, String aType, String aDefault) {
        setName(aName);
        setType(aType);
        setDefaultValue(aDefault);
    }

    public RowDefinition(String aName, String aType, boolean aPrimary) {
        setName(aName);
        setType(aType);
        if(aPrimary){
            setIdentity(true);
            setPrimary(true);
        }
    }

    public RowDefinition(String aName, String aType, RowDefinition aForeign) {
        setName(aName);
        setType(aType);
        setReference(aForeign);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public boolean isPrimary() {
        return primary;
    }

    public void setPrimary(boolean primary) {
        this.primary = primary;
    }

    public boolean isIdentity() {
        return identity;
    }

    public void setIdentity(boolean identity) {
        this.identity = identity;
    }

    public String getDefaultValue() {
        return defaultValue;
    }

    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

    public RowDefinition getReference() {
        return reference;
    }

    public void setReference(RowDefinition reference) {
        this.reference = reference;
    }

    public TableDefinition getTable() {
        return table;
    }

    public void setTable(TableDefinition table) {
        this.table = table;
    }

    public String getQuery(){
        StringBuffer sb = new StringBuffer();
        sb.append(""+getName()+" "+getType());
        if(isIdentity()){
            sb.append(" GENERATED BY DEFAULT AS IDENTITY(START WITH 1)");
        }else{
            if(getDefaultValue()!=null)
                sb.append(" DEFAULT "+getDefaultValue());
        }
        return sb.toString();
    }
}
